package com.atguigu.userservice.controller;


import com.atguigu.commonutils.ClientEntity.ClientMember;
import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.userservice.entity.UcenterMember;
import com.atguigu.userservice.entity.vo.LoginVo;
import com.atguigu.userservice.entity.vo.RegisterVo;
import com.atguigu.userservice.service.UcenterMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author huangqicai
 * @since 2022-10-27
 */
@Api(description = "手动输入信息登录注册")
//原来需要在Controller上面加上@CrossOrigin解决跨域问题，现在gateway已经处理了
@RestController
@RequestMapping("/userservice/member")
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService memberService;

    @ApiOperation(value = "实现用户注册")
    @PostMapping("/userRegister")
    public R userRegister(@RequestBody RegisterVo registerVo){
        return memberService.userRegister(registerVo);
    }


    /*实现用户登录的方法*/
    /*返回的是一个token*/
    @ApiOperation(value = "实现用户登录,返回给前端的是一个token字符串")
    @PostMapping("/userLogin")
    public R userLogin(@RequestBody LoginVo loginVo){
        return memberService.userLogin(loginVo);
    }


    /*根据token获取用户信息*/
    @ApiOperation(value = "根据token获取用户信息,根据前端传递过来的token获取用户信息")
    @GetMapping("/getLoginInfoFront")
    public R getLoginInfoFront(HttpServletRequest request){
        /*获取到token里面的用户id*/
        String userId = JwtUtils.getMemberIdByJwtToken(request);
        /*根据用户id获取用户登录信息*/
        UcenterMember ucenterMember = new UcenterMember();
        ucenterMember=memberService.getLoginInfoFront(userId);
        System.out.println("返回的信息是："+ucenterMember);
        return R.ok().data("ucenterMember",ucenterMember);
    }


    /*根据token获取用户信息*/
    @ApiOperation(value = "这个是远程调用的Client方法")
    @GetMapping("/getLoginInfo/{userId}")
    public ClientMember getLoginInfo(@PathVariable(value = "userId") String userId){
        System.out.println("运行到这里了");
        return memberService.getLoginInfo(userId);
    }





}

